package com.jingyanzi.marketing.service.impl;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.marketing.dao.PresentMapper;
import com.jingyanzi.marketing.domain.PresentDo;
import com.jingyanzi.marketing.entity.MaPresent;
import com.jingyanzi.marketing.entity.MaPresentExample;
import com.jingyanzi.marketing.entity.MaPresentExample.Criteria;
import com.jingyanzi.marketing.query.PresentQuery;
import com.jingyanzi.marketing.result.ResultDO;
import com.jingyanzi.marketing.result.ResultSupport;
import com.jingyanzi.marketing.service.IPresentService;

@Service("maPresentService")
public class PresentServiceImpl extends BaseServiceImpl implements IPresentService {

	@Autowired
	private PresentMapper presentMapper;

	public ResultDO add(PresentDo maPresentDo) {
		MaPresent present = new MaPresent();
		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		present.setName(maPresentDo.getName());
		present.setCode(maPresentDo.getCode());
		present.setIntegral(maPresentDo.getIntegral());
		present.setRegion(StringUtils.join(maPresentDo.getRegion(), ","));
		present.setRegionCode(StringUtils.join(maPresentDo.getRegionCode(), ","));
		if (maPresentDo.getThumbnail() == null) {
			present.setThumbnail("");
		} else {
			present.setThumbnail(maPresentDo.getThumbnail());
		}

		present.setTags(maPresentDo.getTags());
		present.setSurplus(maPresentDo.getSurplus());
		present.setExchange(0);
		present.setCreatetime(Calendar.getInstance().getTime());
		present.setIsDelete(1);
		int r = -1;

		try {
			r = presentMapper.insert(present);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加礼品信息失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加礼品信息失败");
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, present.getId());
		return result;
	}

	public ResultDO modifi(PresentDo maPresentDo) {
		MaPresent present = new MaPresent();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		present.setId(maPresentDo.getId());
		present.setExchange(maPresentDo.getExchange());
		present.setIntegral(maPresentDo.getIntegral());
		present.setSurplus(maPresentDo.getSurplus());
		present.setName(maPresentDo.getName());
		present.setCode(maPresentDo.getCode());
		present.setRegion(StringUtils.join(maPresentDo.getRegion(), ","));
		present.setRegionCode(StringUtils.join(maPresentDo.getRegionCode(), ","));
		present.setThumbnail(maPresentDo.getThumbnail());
		present.setTags(maPresentDo.getTags());
		present.setSurplus(maPresentDo.getSurplus());
		int r = -1;
		try {
			r = presentMapper.updateByPrimaryKeySelective(present);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改礼品信息失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改礼品信息失败");
			result.setSuccess(false);
			return result;
		}
		return result;
	}

	public ResultDO remove(Integer[] id) {
		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;

		for (int i = 0; i < id.length; i++) {
			MaPresent present = new MaPresent();
			present.setId(id[i]);
			present.setIsDelete(-1);
			int r = -1;

			try {
				r = presentMapper.updateByPrimaryKeySelective(present);
			} catch (Exception e) {
				e.printStackTrace();
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除礼品信息失败");
				result.setSuccess(false);
				return result;
			}

			if (r < 1) {
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除礼品信息失败");
				result.setSuccess(false);
				return result;
			}
		}

		return result;
	}

	public ResultDO get(Integer id) {
		MaPresent present = new MaPresent();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		present.setId(id);
		present.setIsDelete(-1);
		try {
			present = presentMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取礼品信息失败");
			result.setSuccess(false);
			return result;
		}

		if (present == null) {
			result.setErrorCode(ResultDO.PRESENT_GET_ERROR);
			result.setErrorMsg("获取礼品信息失败");
			result.setSuccess(false);
			return result;
		}
		PresentDo presentDo = getMaPresentDo(present);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, presentDo);
		return result;
	}

	public ResultDO query(PresentQuery maPresentQuery) {
		ResultSupport result = new ResultSupport();

		List<MaPresent> list = null;

		MaPresentExample example = new MaPresentExample();

		Criteria createCriteria = example.createCriteria();

		
		if(maPresentQuery.getStartPoint() != null && maPresentQuery.getEndPoint() != null) { 
			if(maPresentQuery.getStartPoint() != 0 && maPresentQuery.getEndPoint() != 0) {
				createCriteria.andIntegralBetween(maPresentQuery.getStartPoint(), maPresentQuery.getEndPoint());
			}
		}
		

		if(maPresentQuery.getStartPoint() != null) {
			if(maPresentQuery.getStartPoint() != 0) {
				createCriteria.andIntegralGreaterThanOrEqualTo(maPresentQuery.getStartPoint());
			}
		}
		

		if(maPresentQuery.getEndPoint() != null) {
			if(maPresentQuery.getEndPoint() != 0) {
				createCriteria.andIntegralLessThanOrEqualTo(maPresentQuery.getEndPoint());
			}
		}
		
		// 礼品名称
		if (StringUtils.isNotBlank(maPresentQuery.getName())) {
			createCriteria.andNameLike("%" + maPresentQuery.getName() + "%");
		}
		createCriteria.andIsDeleteEqualTo(1);

		List<PresentDo> presentDos = null;

		if (maPresentQuery.isPage()) {
			example.setLimitByPage(maPresentQuery.getStart() + "," + maPresentQuery.getPageRows());
			long count = presentMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		try {
			list = presentMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取礼品信息失败");
			result.setSuccess(false);
			return result;
		}
		presentDos = getMaPresentDo(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, presentDos);

		return result;
	}

}
